package com.animationsp.acs.action;


import com.animationsp.acs.util.Constant;
import com.animationsp.acs.util.ErrorMap;
import com.animationsp.acs.util.MyActionSupport;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.rs.RSClient;


public class RenameFileAction extends MyActionSupport {
	private String srcKey;
	private String destKey;
	
	/**
	 * @see http://docs.qiniu.com/api/v6/rs.html#move
	 */
	@Override
	public String execute() throws Exception {
		if(destKey == null || destKey.equals("")){
			this.setErr("文件名不能为空");
		}
		
		srcKey = new String(srcKey.getBytes("iso8859-1"),"utf-8"); // tomcat默认编码是iso8859-1，所以收到的是该编码的参数，先转成utf-8		
		System.out.println("rename srcKey: "+srcKey);
		destKey = new String(destKey.getBytes("iso8859-1"),"utf-8"); // tomcat默认编码是iso8859-1，所以收到的是该编码的参数，先转成utf-8		
		System.out.println("rename destKey: "+destKey);
		
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        RSClient client = new RSClient(mac);
        CallRet ret = client.move(Constant.BUCKET, srcKey, Constant.BUCKET, destKey);
		System.out.println("move(rename) "+ret.statusCode + " " + ret.response + " "+ ret.exception);
		if(!ret.ok()){
			this.setErr(ErrorMap.ERROR_MAP.get(ret.statusCode) == null?"未知错误"+ret.statusCode:ErrorMap.ERROR_MAP.get(ret.statusCode));
		}
		return SUCCESS;
	}
	
	public static void main(String[] args){
		Mac mac = new Mac("lQvrG3e4dV9w8szU32Dq0SdkcJMTVIRQ_QjGIZIz", "nygi6Ew2RLw3W0fzhBcjtEXg15_D0nIUgVcnA81l");
        RSClient client = new RSClient(mac);
        CallRet ret = client.move(Constant.BUCKET, "aaaa", Constant.BUCKET, "1111");
        System.out.println("move(rename) "+ret.statusCode + " " + ret.response + " "+ ret.exception);
		
	}

	public String getSrcKey() {
		return srcKey;
	}

	public void setSrcKey(String srcKey) {
		this.srcKey = srcKey;
	}

	public String getDestKey() {
		return destKey;
	}

	public void setDestKey(String destKey) {
		this.destKey = destKey;
	}
	
	
}
